package com.app.util.database.mybatis;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.app.util.common.ObjUtil;



/**
 * 分页类
 */
public class Page implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5533842459350912797L;
	

	private boolean entityOrField;	//true:需要分页的地方，传入的参数就是Page实体；false:需要分页的地方，传入的参数所代表的实体拥有Page属性
	private List<?> dataList;
	private PageData pd = new PageData();
	
	private boolean usePage = true;		//是否开启分页  默认开启
	
	
	//组合成 一组 独立的 以and连接的查询条件
	private List<QueryField> queryAnds = new ArrayList<QueryField>();
	
	//组合成一组 独立的以or连接的查询条件
	private List<QueryField> queryOrs = new ArrayList<QueryField>();
	
	
	private int totalPage;		//总页数
	private int totalResult;	//总记录数
	private int currentResult;	//当前记录起始索引
	
	
	private String keyName;		//查询字段		
	private String keywords;	//查询关键字
	private Integer page = 1;	//当前页
	private Integer rows = 20;	//每页显示数量
	private String sort;		//排序字段
	private String order;		//排序方式
	private List<Map<String, Object>> listMap;//查询不确定结果集 map
	private String strTableName;//查询不确定表名
	
	public Page(){
		
	}
	
	public int getTotalPage() {
		if(totalResult%rows==0)
			totalPage = totalResult/rows;
		else
			totalPage = totalResult/rows+1;
		return totalPage;
	}
	
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	public int getTotalResult() {
		return totalResult;
	}
	
	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}
	
	public int getCurrentPage() {

		if(page<=0)
			page = 1;
		return page;
	}
	
	public void setCurrentPage(int page) {
		this.page = page;
	}
	
	
	public int getCurrentResult() {
		currentResult = (getCurrentPage()-1)*getRows();
		if(currentResult<0)
			currentResult = 0;
		return currentResult;
	}
	
	public void setCurrentResult(int currentResult) {
		this.currentResult = currentResult;
	}
	
	public boolean isEntityOrField() {
		return entityOrField;
	}
	
	public void setEntityOrField(boolean entityOrField) {
		this.entityOrField = entityOrField;
	}
	
	public PageData getPd() {
		return pd;
	}

	public void setPd(PageData pd) {
		this.pd = pd;
	}	

	
	
	@SuppressWarnings("rawtypes")
	public List getDataList() {
		return dataList;
	}

	public void setDataList(List<?> dataList) {
		this.dataList = dataList;
	}

	
	
	public String getKeyName() {
				
		return ObjUtil.trim(keyName); 
	}

	public void setKeyName(String keyName) {
		keyName = ObjUtil.trim(keyName);
		this.keyName = keyName;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public String getSort() {
		
		
		return sort;
	}

	public void setSort(String sort) {
		
		boolean isSqlStr = ObjUtil.sqlValidateWithSpace(sort);
		if(isSqlStr) {
			this.sort = "";
		}else {
			this.sort = sort;
		}
		
		
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		//去除可能的安全隐患 
		boolean isSqlStr = ObjUtil.sqlValidateWithSpace(order);
		if(isSqlStr) {
			this.order = "";
		}else {
			this.order = order;
		}
		
		
	}
	
	
	public void put(String key,Object value){
		pd.put(key, value);
	}
	
	public Object get(String key){
		return pd.get(key);
	}

	public List<QueryField> getQueryAnds() {
		return queryAnds;
	}

	public void setQueryAnds(List<QueryField> queryAnds) {
		this.queryAnds = queryAnds;
	}

	public List<QueryField> getQueryOrs() {
		return queryOrs;
	}

	public void setQueryOrs(List<QueryField> queryOrs) {
		this.queryOrs = queryOrs;
	}

	/**
	 * 往queryAnd中添加查询项，默认为QueryType.markEQ
	 * @param queryFiled
	 */
	public void addQueryAnd(String field,Object value){
		
		this.getQueryAnds().add(new QueryField(field,QueryType.markEQ,value));
	}
	
	/**
	 * 往queryAnd中添加查询项
	 * @param queryFiled
	 */
	public void addQueryAnd(String field,String QueryType,Object value){
		
		this.getQueryAnds().add(new QueryField(field,QueryType,value));
	}
	
	public void addQueryAnd(QueryField qeryField){
		
		this.getQueryAnds().add(qeryField);
	}
	/**
	 * 清空queryAnd中的查询项
	 */
	public void clearQueryAnd(){
		this.getQueryAnds().clear();
	}
	
	
	/**
	 * 往queryOr中添加查询项
	 * @param queryFiled
	 */
	public void addQueryOr(String field,String queryType,Object value){
		
		this.getQueryOrs().add(new QueryField(field,queryType,value));
	}
	
	public void addQueryOr(QueryField qeryField){
		
		this.getQueryOrs().add(qeryField);
	}
	
	/**
	 * 往queryOr中添加查询项，默认为QueryType.markEQ
	 * @param queryFiled
	 */
	public void addQueryOr(String field,Object value){
		
		this.getQueryOrs().add(new QueryField(field,QueryType.markEQ,value));
	}
	
	
	public void removeQueryAnd(String field) {
		List<QueryField> list = this.getQueryAnds();
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getField().equals(field)) {
				list.remove(i);
			}
		}
	}
	
	public void removeQueryOr(String field) {
		List<QueryField> list = this.getQueryOrs();
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getField().equals(field)) {
				list.remove(i);
			}
		}
	}


	/**
	 * 清空queryOr中的查询项
	 */
	public void clearQueryOr(){
		
		this.getQueryOrs().clear();
	}

	public boolean isUsePage() {
		return usePage;
	}

	/**
	 * 是否分页
	 * @param usePage
	 */
	public void setUsePage(boolean usePage) {
		this.usePage = usePage;
	}

	public List<Map<String, Object>> getListMap() {
		return listMap;
	}

	public void setListMap(List<Map<String, Object>> listMap) {
		this.listMap = listMap;
	}

	public String getStrTableName() {
		return strTableName;
	}

	public void setStrTableName(String strTableName) {
		this.strTableName = strTableName;
	}

	
}
